package jmine.tec.web.pages.app;

import images.ImageResources;

import java.math.BigDecimal;

import jmine.tec.datadigester.web.pages.importacao.Importacao;
import jmine.tec.persist.web.pages.auth.Authorization;
import jmine.tec.rtm.impl.web.pages.diagnosticador.ConsultaDiagnosticador;
import jmine.tec.web.wicket.application.JMineWicketWebApplication;
import jmine.tec.web.wicket.component.autocomplete.NaturalKeyAutoCompleteEntity;
import jmine.tec.web.wicket.component.autocomplete.NaturalKeyAutoCompleteEntityConverter;
import jmine.tec.web.wicket.converter.BigDecimalConverter;
import jmine.tec.web.wicket.converter.BooleanSimNaoConverter;
import jmine.tec.web.wicket.converter.DateConverter;
import jmine.tec.web.wicket.converter.PercentageBigDecimal;
import jmine.tec.web.wicket.converter.PercentageConverter;
import jmine.tec.web.wicket.converter.TecDateConverter;
import jmine.tec.web.wicket.pages.example.crud.CrudPageExample;
import jmine.tec.web.wicket.pages.example.execute.ExecutePageExample;
import jmine.tec.web.wicket.pages.example.executeajaxselection.ExecutePageAjaxExample;
import jmine.tec.web.wicket.pages.example.form.FormPageExample;
import jmine.tec.web.wicket.pages.example.list.ListPageExample;
import jmine.tec.web.wicket.pages.example.report.ReportPageExample;
import jmine.tec.web.wicket.pages.main.Home;
import jmine.tec.web.wicket.pages.main.Login;
import jmine.tec.web.wicket.pages.main.Logout;
import jmine.tec.web.wicket.pages.main.Timeout;
import jmine.tec.web.wicket.security.SecureSession;

import org.apache.wicket.IConverterLocator;
import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.authorization.IAuthorizationStrategy;
import org.apache.wicket.markup.html.pages.AccessDeniedPage;
import org.apache.wicket.request.target.coding.QueryStringUrlCodingStrategy;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.apache.wicket.util.convert.ConverterLocator;

import bancosys.tec.security.SecurityManager;
import bancosys.tec.utils.date.Date;

/**
 * @author lundberg
 */
public class JMineTestApplication extends JMineWicketWebApplication {

    private SecurityManager securityManager;

    private IAuthorizationStrategy authorizationStrategy;

    /**
     * {@inheritDoc}
     */
    @Override
    public Class<? extends Page> getHomePage() {
        return Home.class;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Session newSession(Request request, Response response) {
        SecureSession secureSession = new SecureSession(request);
        secureSession.setSecurityManager(this.securityManager);
        return secureSession;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void init() {
        super.init();
        this.addComponentInstantiationListener(new SpringComponentInjector(this));
        this.getSharedResources().putClassAlias(ImageResources.class, "images");
        this.getSecuritySettings().setAuthorizationStrategy(this.authorizationStrategy);
        this.getApplicationSettings().setAccessDeniedPage(AccessDeniedPage.class);

        this.mount(new QueryStringUrlCodingStrategy("logout", Logout.class));
        this.mount(new QueryStringUrlCodingStrategy("login", Login.class));
        this.mount(new QueryStringUrlCodingStrategy("accessDenied", AccessDeniedPage.class));
        this.mount(new QueryStringUrlCodingStrategy("home", Home.class));
        this.mount(new QueryStringUrlCodingStrategy("timeout", Timeout.class));
        this.mount(new QueryStringUrlCodingStrategy("rtm", ConsultaDiagnosticador.class));
        this.mount(new QueryStringUrlCodingStrategy("auth", Authorization.class));
        this.mount(new QueryStringUrlCodingStrategy("import", Importacao.class));
        this.mount(new QueryStringUrlCodingStrategy("crudExample", CrudPageExample.class));
        this.mount(new QueryStringUrlCodingStrategy("executeExample", ExecutePageExample.class));
        this.mount(new QueryStringUrlCodingStrategy("executeAjaxExample", ExecutePageAjaxExample.class));
        this.mount(new QueryStringUrlCodingStrategy("formExample", FormPageExample.class));
        this.mount(new QueryStringUrlCodingStrategy("listPageExample", ListPageExample.class));
        this.mount(new QueryStringUrlCodingStrategy("reportExample", ReportPageExample.class));
        // this.mount(new QueryStringUrlCodingStrategy("import", Importacao.class));
        // Importacao.class
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected IConverterLocator newConverterLocator() {
        ConverterLocator converterLocator = new ConverterLocator();
        converterLocator.set(BigDecimal.class, new BigDecimalConverter());
        converterLocator.set(java.util.Date.class, new DateConverter());
        converterLocator.set(Date.class, new TecDateConverter());
        converterLocator.set(PercentageBigDecimal.class, new PercentageConverter());
        converterLocator.set(Boolean.class, new BooleanSimNaoConverter());
        converterLocator.set(NaturalKeyAutoCompleteEntity.class, new NaturalKeyAutoCompleteEntityConverter());
        return converterLocator;
    }

    /**
     * @param securityManager the securityManager to set
     */
    public void setSecurityManager(SecurityManager securityManager) {
        this.securityManager = securityManager;
    }

    /**
     * @param authorizationStrategy the authorizationStrategy to set
     */
    public void setAuthorizationStrategy(IAuthorizationStrategy authorizationStrategy) {
        this.authorizationStrategy = authorizationStrategy;
    }

}
